//
//  icanplanViewController.m
//  icanplan
//
//  Created by eric on 11-9-12.
//  Copyright 2011年 eric. All rights reserved.
//

#import "icanplanViewController.h"

@implementation icanplanViewController

@synthesize usernameField;
@synthesize passwordField;
@synthesize sliderLabel;
@synthesize leftSwitch;
@synthesize rightSwitch;
@synthesize switchView;
@synthesize deleteButton;

-(IBAction)textFieldDoneEditing:(id)sender
{
    [sender resignFirstResponder];
}

-(IBAction)backgroundClicked:(id)sender
{
    [usernameField resignFirstResponder];
    [passwordField resignFirstResponder];
}

-(IBAction)sliderChanged:(id)sender
{
    UISlider *slider = (UISlider *)sender;
    int sliderValue = (int)(slider.value + 0.5f);
    NSString *newText = [[NSString alloc] initWithFormat:@"%d",sliderValue];
    sliderLabel.text = newText;
    [newText release];
}

-(IBAction)switchChanged:(id)sender
{
    UISwitch *whichSwitch = (UISwitch *)sender;
    BOOL setup = [whichSwitch isOn];
    [leftSwitch setOn:setup animated:YES];
    [rightSwitch setOn:setup animated:YES];
}

-(IBAction)toggleShowHide:(id)sender
{
    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    NSInteger segment = [segmentedControl selectedSegmentIndex];
    
    if(segment == kShowSegmentIndex) {
        [switchView setHidden:NO];
    }
    else {
        [switchView setHidden:YES];
    }
}

-(IBAction)deleteData:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                 initWithTitle:@"确定要删除么"
                                 delegate:self
                                 cancelButtonTitle:@"不了，谢谢"
                                 destructiveButtonTitle:@"是的，当然"
                                 otherButtonTitles:@"还是在考虑一下",nil];
    [actionSheet showInView:self.view];
    [actionSheet release];
    
}

-(void)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == [actionSheet destructiveButtonIndex]) {
        NSString *msg = nil;
        if(usernameField.text.length > 0) {
            msg = [[NSString alloc] initWithFormat:
                   @"%@，您的用户数据已经被删除",
                   usernameField.text];
        }
        else {
            msg = [[NSString alloc] initWithFormat:
                   @"您尚未输入用户名"];
        }
        
        UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"Pressed Something"
                                  message:msg
                                  delegate:self
                                  cancelButtonTitle:@"确定"
                                  otherButtonTitles:nil];
        [alert show];
        [alert release];
        [msg release];
    }
}


- (void)dealloc
{
    [usernameField release];
    [passwordField release];
    [sliderLabel release];
    [leftSwitch release];
    [rightSwitch release];
    [switchView release];
    [deleteButton release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

@end
